Required: x, y
Optional: aplpha, color, fill, shape, size
ggplot(data, aes(x=gdpPercap, y=lifeExp, fill=continent, size=pop)) +
geom_point(color="black", alpha=0.7, shape=25) geom_point(position="jitter")
ggplot(data, aes(x=gdpPercap, y=lifeExp)) +
geom_point() +
scale_x_log10() +
geom_smooth(method="lm")ggplot(data, aes(x=gdpPercap, y=lifeExp, color=continent)) +
geom_point() +
scale_x_log10() +
geom_smooth(method="lm", se=FALSE) +
geom_smooth(method="lm", se=FALSE, aes(group=1, color="Overall Global"), linetype=2) +
scale_color_manual("Region", values= c("firebrick3","orange","#1B9E77","royalblue3","mediumpurple4","black"))Shows the binned distribution of a continuous variable
Required: x
See the section on aesthetics for bar graphs, below
Optional: bins or binwidth
Optional: y (which makes it frequency instead of count)
Shows the distribution of a discrete variable
Required: x
Shows a selected statistic for each category of the variable selected
Required: x, y
data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp)) %>%
ggplot(aes(x = continent, y = mean_life)) +
geom_col()data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp),
sd_life = sd(lifeExp)
) %>%
ggplot(aes(x = continent, y = mean_life)) +
geom_col(fill ="grey50") +
geom_errorbar(aes(ymin=(mean_life - sd_life),
ymax=(mean_life + sd_life),
width=0.2
)
)gapminder %>%
group_by(year, continent) %>%
summarize(tot_pop = sum(as.numeric(pop))) %>%
ggplot(aes(x = year, y = tot_pop, fill = continent)) +
geom_col()gapminder %>%
group_by(year, continent) %>%
summarize(tot_pop = sum(as.numeric(pop))) %>%
ggplot(aes(x=year, y=tot_pop, fill=continent)) +
geom_col(position="fill")gapminder %>%
group_by(year, continent) %>%
summarize(tot_pop = sum(as.numeric(pop))) %>%
filter(year %in% c(1957, 1977, 1997)) %>%
ggplot(aes(x = continent, y = tot_pop, fill = factor(year))) +
geom_col(position = "dodge")Optional: aplpha, color, fill
data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp)) %>%
ggplot(aes(x = continent, y = mean_life)) +
geom_col(color = "darkblue", fill = "lightblue")data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp),
sd_life = sd(lifeExp)
) %>%
ggplot(aes(x = reorder(continent, mean_life), y = mean_life)) +
geom_col()data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp),
sd_life = sd(lifeExp)
) %>%
ggplot(aes(x = reorder(continent, -mean_life), y = mean_life)) +
geom_col()gapminder %>%
group_by(year, continent) %>%
summarize(tot_pop = sum(as.numeric(pop))) %>%
ggplot(aes(x = year, y = tot_pop, fill = continent)) +
geom_col(position = position_stack(reverse = TRUE)) +
guides(fill = guide_legend(reverse = TRUE))data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp),
sd_life = sd(lifeExp)
) %>%
ggplot(aes(x = continent, y = mean_life)) +
geom_col(width = 0.5)data %>%
group_by(continent) %>%
summarize(mean_life = mean(lifeExp),
sd_life = sd(lifeExp)
) %>%
ggplot(aes(x = continent, y = mean_life)) +
geom_col(width = 1)Required: x, y
gapminder %>%
group_by(year) %>%
summarize(pop = sum(as.numeric(pop))) %>%
ggplot(aes(x=year, y=pop)) +
geom_line()gapminder %>%
group_by(year) %>%
summarize(pop = sum(as.numeric(pop))) %>%
ggplot(aes(x=year, y=pop)) +
geom_line() +
geom_point()Optional: color, size, alpha, linetype
gapminder %>%
group_by(year, continent) %>%
summarize(pop = sum(as.numeric(pop))) %>%
ggplot(aes(x = year, y = pop, color = continent)) +
geom_line(size = 1, alpha = 0.6)gapminder %>%
group_by(year, continent) %>%
summarize(pop = sum(as.numeric(pop))) %>%
ggplot(aes(x=year, y=pop, fill=continent)) +
geom_area()gapminder %>%
group_by(year, continent) %>%
summarize(pop = sum(as.numeric(pop))) %>%
ggplot(aes(x=year, y=pop, fill=continent)) +
geom_area(position="fill")recessions <- tibble(
begin = c("1969-12-01", "1973-11-01", "1980-01-01",
"1981-07-01", "1990-07-01", "2001-03-01"
),
end = c("1970-11-01", "1975-03-01", "1980-07-01",
"1982-11-01", "1991-03-01", "2001-11-01")
) %>%
mutate(begin = as.Date(begin),
end = as.Date(end)
)
ggplot(economics, aes(x = date, y = unemploy/pop)) +
geom_rect(data = recessions, inherit.aes = FALSE,
aes(xmin = begin, xmax = end, ymin = -Inf, ymax = Inf),
fill = "red", alpha = 0.2) +
geom_line() +
labs(
x = "",
y = "Unemployment Rate",
title = "Relationship Between Recessions and Unemployment"
)gapminder %>%
filter(continent == "Asia", year == 2007) %>%
ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) +
geom_point(size = 3) +
theme_bw() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey60", linetype = "dashed")
)my_fun <- function(xvar) {
1 / (1 + exp(-xvar + 10))
}
ggplot(data.frame(x = c(0, 20)), aes(x = x)) +
stat_function(fun = my_fun, geom = "line")figure +
labs(
title = "This is my title",
subtitle = "This is my subtitle",
x = "This is my x-axis",
y = "This is my y-axis",
fill = "This is my fill legend",
color = "This is my color legend",
caption = "This is my caption"
)element_text()
color =size =face = "plain"/"italic"/"bold"/"bold.italic"family =hjust =vjust =figure +
theme(
plot.title = element_text(face = "bold.italic"),
plot.subtitle = element_text(color = "darkgray"),
plot.caption = element_text(hjust = 0),
axis.title.x = element_text(hjust = 1)
)The scale_ functions include whether the axis is _x_ or _y_ and whether the variable plotted is _discrete() or _continuous():
scale_x_discrete()scale_y_continuous()Caution, this will drop data outside of the limits (including bars)
Caution, this will still plot points outside of the range of the breaks leaving the rest of the y axis blank
scales formats:
commapercentscientific| Aesthetic | Description | Values |
|---|---|---|
| x | x-axis data | |
| y | y-axis data | |
| color | color of dots, outline of other shapes | |
| fill | fill color | Same as color |
| alpha | transparency | |
| size | diameter of points, thickness of lines | |
| linetype | line dash pattern | |
| labels | text on a plot or axes | see below |
| shape | shape of dot |
Brewer
scale_color_brewer(palette = "Spectral")
scale_fill_brewer(palette = "Spectral")
ggplot(data, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_color_brewer(palette = "Spectral") ###Manual
scale_color_manual(values = c("color", "color", "color"))
scale_fill_manual(values = c("color", "color", "color"))
ggplot(data, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_color_manual(values = c("deeppink",
"deepskyblue4",
"seagreen4",
"darkorchid1",
"coral3"
)
) facet_wrap(~)
facet_grid(~)
See section on Patchwork for more
gapminder_figure +
annotate("text",
x = 14000,
y = 57,
label = "This Country",
vjust = 0,
hjust = 0,
color = "darkgreen",
size = 3,
fontface = "bold.italic")gapminder_figure +
geom_hline(yintercept = 60,
color = "blue",
linetype = 2) +
geom_vline(xintercept = 25000,
color = "purple",
linetype = 1)heights(row1, row2, . . .)
widths(col1, col2, . . .)
p1 + labs(subtitle = "A. This is Plot 1") +
p2 + labs(subtitle = "B. This is Plot 2") +
plot_annotation(title = "A great plot!")p1 + p2 +
plot_annotation(
title = "Here is a Complexly Formatted, Simple Graph",
caption = str_glue("Sources: ggplot\n\nNote: This is just basic analysis as an example."),
theme = theme(
plot.title = element_text(face = "bold", hjust = 1),
plot.caption = element_text(face = "italic", hjust = 0)
)
)